package com.cjq.controller;

import com.cjq.pojo.Studentssm;
import com.cjq.service.StudentService;
import com.cjq.vo.QueryVO2;
import com.cjq.vo.ResultVO;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("student")
@ResponseBody       //返回对象类型
public class StudentController {

    @Resource
    private StudentService studentService;

    //多条件分页查询
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ResultVO<Studentssm> queryByPage(Integer pageNum, Integer pageSize, QueryVO2 vo){

        if (pageNum==null || pageNum<=0){
            pageNum = 1;
        }
        if (pageSize==null || pageSize<=0){
            pageSize=5;
        }
        PageInfo<Studentssm> studentssmPageInfo = studentService.queryByPage(pageNum, pageSize, vo);
        return new ResultVO<>(studentssmPageInfo);
    }

    //添加球队
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public ResultVO<Studentssm> addStudent(Studentssm studentssm){
        int i = studentService.addStudent(studentssm);
        if (i==1){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"添加失败！服务器内部异常，请稍后再试！");
    }


    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)   //这个和上面的访问路径是一样的，但是访问的方式一个是put一个是delete，所以还是能找到对应的方法
    public ResultVO<Studentssm> deleteTeam(@PathVariable("id") Integer stuid){
        int i = studentService.deleteStudent(stuid);
        if (i==1){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"删除失败！服务器内部异常，请稍后再试！");
    }


    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public Studentssm querById(@PathVariable("id") Integer stuid){

        return studentService.selectStudent(stuid);
    }
    //根据主键动态更新球队信息
    @RequestMapping(value = "{stuid}",method = RequestMethod.PUT)
    public ResultVO<Studentssm> updateTeam(@PathVariable("stuid") Integer stuid,Studentssm studentssm){
        //因为前端的页面设置了teamId那个input标签为disable  传进来的team对象是没有teamId的值的
        studentssm.setStuid(stuid);
        System.out.println(studentssm);
        int i = studentService.updateStudent(studentssm);
        if (i==1){
            return new ResultVO<>();
        }
        return new ResultVO<>(500,"添加失败！服务器内部异常，请稍后再试！");
    }

}
